<?php

//require_once 'L324/data/mysql/MySQLSession.php';
require_once 'classes/common/util/ConfigurationManager.class.php';

/**
 * A la obligation d'établir de connections à la BD.
 *
 * @package integration
 */
class DataSourceFactory {

// @TODO Utiliser ces variables pour gerer plusieurs connections de BDD
    
//    const _DB_CONN_1    = 1;
//    const _DB_CONN_2    = 2;
    
    static private $_instanceOfFactory = null;
    static private $_baseBddInfo       = null; 

    //*************************************************************************
    //*************************************************************************
    
    // private constructor
    private function __construct(){
        self::$_baseBddInfo = ConfigurationManager::getInstance()->get('bdd_info');
    }


    /**
     * Enter description here...
     *
     * @return DataSourceFactory
     */
    public static function getInstance() {
        if (self::$_instanceOfFactory == null) {
            self::$_instanceOfFactory = new self;
        }

        return self::$_instanceOfFactory;
    }
    
    
    
    /**
     * 
     * @param $useUtf
     * @param $host
     * @param $username
     * @param $password
     * @return unknown_type
     */
    public static function & getConnectionSimple($useUtf8=true, $host=null, $username=null, $password=null) {
        // Si l'info de connection n'est pas fourni, utliser la conf de base.
        if ($host === null) {
            $host       = self::$_baseBddInfo['hote'];
            $username   = self::$_baseBddInfo['nom_utilisateur'];
            $password   = self::$_baseBddInfo['mot_de_passe'];
        }
        
        // MySQL Connection
        try {

            /**
             * $newLink = 'true' indique l'utilisation d'une nouvelle connexion à chaque fois.  
             */
            $conn = mysql_connect($host, $username, $password, $newLink=true);
            if ( ! $conn) {
                // Exception !
                throw new Exception ();
            }

            if ($useUtf8) {
                // Etabli l'encodage par défaut à UTF-8
                mysql_query("SET NAMES utf8");
            }

        } catch (Exception $e) {
            throw new Exception ();
        }


        return $conn;
    } 
    
    /**
     * 
     * @param $datebaseName
     * @param $useUtf8      vrai ou faux
     * @param $host
     * @param $username
     * @param $password
     * @return unknown_type
     */
    public static function getConnectionBdd($datebaseName, $useUtf8, $host=null, $username=null, $password=null) {

        if ($datebaseName === null) {
            throw new Exception();    
        }

        // MySQL Connection
        $conn = & self::getConnectionSimple($useUtf8, $host, $username, $password);

        try {
            if ( ! mysql_select_db($datebaseName, $conn) ) {
                //echo 'ERR ' . mysql_error();
                throw new Exception ();
            } 

        } catch (Exception $e) {
            throw new Exception ();
        }


        return $conn;
    }

    /**
     * 
     * @param $useUtf8
     * @return unknown_type
     */
    public static function getConnection($useUtf8=true) {
        $database   = self::$_baseBddInfo['nom_bdd'];

        return self::getConnectionBdd($database, $useUtf8);
    }
    
    /**
     * Connexion à la Bdd Référence
     * @return ISession
     */
    public static function getSession() {
        return new MySQLSession( self::getConnection() );
    }
    

} ?>